package com.cafuc.javaEE.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {
    private static String user = "";
    private static String password = "";
    private static String url = "";
    private static String driver = "";
    private static Connection conn = null;

//    初始化连接属性
    static {
        driver = ConfigUtil.properties.getProperty("jdbc.driverClassName");
        url = ConfigUtil.properties.getProperty("jdbc.url");
        user = ConfigUtil.properties.getProperty("jdbc.username");
        password = ConfigUtil.properties.getProperty("jdbc.password");
}

//获取数据库连接
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName(driver);
        conn = DriverManager.getConnection(url, user, password);
        return conn;
    }

//    关闭数据库连接
    public static void closeConnection(Connection conn) throws SQLException {
        if(conn != null){
            conn.close();
        }
    }
}
